<?php
if (!defined('MVC_ROOT_DIR'))  die('Hacking attempt');
class Files 
{	
	/**
	 * 读取配置文件
	 *
	 * @param string $par
	 * @param string $path
	 * @return string
	 */
	public static function configLoad($par,$path)
	{
		$arr = parse_ini_file($path,true);			
		if(count(@$arr[$par])>0){
			return $arr[$par];
		}else{
			return false; 
		}
	}
    public static function getConfig($file,$config,$parm){
	    $array = Files::configLoad($config,MVC_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.$file);
		if ($array==false){
			errors(1001);
		}
		return $array[$parm];
		
	}
	/**
	 * 写日志
	 *
	 * @param string $msg
	 * @param string $type
	 * @return boolean
	 */
	public static function logWrite($msg,$type='system')
	{
		$logDir = MVC_LOG_DIR.DIRECTORY_SEPARATOR.date('Ymd');		
		$file = $logDir.DIRECTORY_SEPARATOR.$type.date("H").'.log';
		if(!file_exists($logDir)){
			mkdir($logDir,0777,1);
		}
		return file_put_contents($file,date("Y-m-d H:i:s")."\t".$msg." \r\n",FILE_APPEND);
		
	}
	public static function writeHtml($dir,$filedir,$content)
	{		
	 	$dir = MVC_WEB_PATH.$dir;
	 	$filedir = MVC_WEB_PATH.$filedir;
		
	 	if(!file_exists($dir)){	 		
	 		mkdir($dir,0777,1);
	 	}
		return 	file_put_contents($filedir,$content);
	 	
	}
	
	/**
	 * 写静态文件
	 *
	 * @param string $dir
	 * @param string $file
	 */
	public static function makeHtml($dir,$file)
	{
		if(substr($dir,0,-1)!='/'){
			$dir = $dir.'/';
		}
		$file = str_replace('/','',$file);
		$out = ob_get_contents();
		mkdir($dir,0777,1);
		file_put_contents($dir.$file,$out);	
	} 
	/**
	 * 加载系统库类 lib下和model下CLASS文件
	 *
	 * @param string $class
	 * @return object
	 */
	public static  function factory($class,$type='require')
	{		
		$file = MVC_ROOT_DIR.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.$class.'.class.php';
		if (file_exists($file)) {
			
			if ($type=='new'){
				require_once $file;
				$class = ucfirst($class);
				return new $class();
			}else{
				return require_once $file;
			}			
		}
		$file = MVC_ROOT_DIR.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'model'.DIRECTORY_SEPARATOR.$class.'.class.php';
		if (file_exists($file)) {
			if ($type=='new'){
				require_once $file;
				$class = ucfirst($class);
				return new $class();
			}else{
				return require_once $file;
			}			
		}		
	}
	/**
	 * 载入插件
	 *
	 * @param string $dir
	 * @return object
	 */
	public static  function loadPlugin($dir)
	{
		if (preg_match('/[-.]php$/',$dir)){
			$file = MVC_ROOT_DIR.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.$dir;
		}else{
			$file = MVC_ROOT_DIR.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.$dir.'.php';
			if (!file_exists($file)){
			    $file = MVC_ROOT_DIR.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.$dir.'.class.php';
			}
		}
		if (file_exists($file)){
		    return require_once $file;
		}else{
		    errors(2004,'zh',$file);
		}
	}
	
	private $arrimg = array();
	
	public function getSysImg($dir,$getThumb=false){
	    
	   
	    $handle=opendir($dir);
	    while (false !== ($file = readdir($handle)))
	    {
	        if (str_replace('..','',$file)==$file) {
	            $file1=$dir.DIRECTORY_SEPARATOR.$file;
	            if (is_dir($file1)){
	                if (str_replace('.','',$file1)==$file1) {
	                    $this->getSysImg($file1,$getThumb);
	                }
	            }
	            if (isImg($file1) ){
	                if ($getThumb){
	                    if (isThumb($file1)){
    	                    $tmpFile = str_replace(MVC_ROOT_DIR."\\",'',$file1);
    	                    $tmpFile = str_replace("\\",'/',$tmpFile);
    	                    $this->arrimg[]=$tmpFile;
	                    }
	                }else{
	                    $tmpFile = str_replace(MVC_ROOT_DIR."\\",'',$file1);
	                    $tmpFile = str_replace("\\",'/',$tmpFile);
	                    $this->arrimg[]=$tmpFile;
	                }
	            }
	        }
	    }
	    return $this->arrimg;
	}
}
